﻿using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace OpenAuth.App.TimeTask
{
    /// <summary>
    /// 定时检查是否有违规日程需生成服务
    /// </summary>
    public class CheckViolationService : BackgroundService
    {
        private readonly ILogger _logger;
        private readonly CheckViolationApp _app;
        public CheckViolationService(ILogger<CheckViolationService> logger, CheckViolationApp app)
        {
            this._logger = logger;
            this._app = app;
        }


        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                _logger.LogInformation("定时检查违规日程服务开始");
                //执行任务
                _app.CheckViolation();
                _logger.LogInformation("违规日程检查定时服务结束");
                await Task.Delay(1200 * 1000, stoppingToken); //间隔1200秒循环
            }
        }


        public override void Dispose()
        {
            base.Dispose();
        }
    }
}
